<?php
class UserGroupModel extends Model {
	protected	$tableName	=	'user_group';
	protected	$fields		=	array (0 => 'uid', 1 => 'uname', 2 => 'gid', 3 => 'gname', '_autoinc' => false);

	//获取所有用户组
	public function getGroups($showDetail=true) {
		$result	=	$this->field('gid,gname')->group('gid')->findAll();
		if(!$result){
			return false;
		}else{
			if($showDetail){
				return $result;
			}else{
				//只输出GID
				foreach($result as $v){
					$group[]	=	$v['gid'];
				}
				return $group;
			}
		}
	}

	//获取一个用户的组
	public function getUserGroups($uid,$showDetail=false) {
		$map['uid']	=	$uid;
		$result	=	$this->field('gid,gname')->where($map)->findAll();
		if(!$result){
			return false;
		}else{
			if($showDetail){
				//输出详细信息
				return $result;
			}else{
				//只输出GID
				foreach($result as $v){
					$group[]	=	$v['gid'];
				}
				return $group;
			}
		}
	}

	//获取一个组的用户
	public function getGroupUsers($gid=190,$showDetail=false) {
		$map['gid']	=	$gid;
		$result	=	$this->field('*')->where($map)->findAll();
		if(!$result){
			return false;
		}else{
			if($showDetail){
				//输出详细信息
				return $result;
			}else{
				//只输出UID
				foreach($result as $v){
					$user[]	=	$v['uid'];
				}
				return $user;
			}

		}
	}
	
	//判断一个用户是否是后台管理员
	public function isAdmin($uid) {

		$uid	=	intval($uid);

		$userInGroup = $this->inGroup($uid,1);

		if($userInGroup){
			return true;
		}else{
			return false;
		}

	}

	//判断一个用户是否在一个组内
	public function inGroup($uid,$gid=190) {
		$map['uid']	=	$uid;
		$map['gid']	=	$gid;
		$result	=	$this->field('uid')->where($map)->find();
		if(!$result){
			return false;
		}else{
			return true;
		}
	}

	//把一个用户添加到一个组内
	public function addToGroup($uid,$gid=190){
		$map['uid']		=	intval($uid);
		$map['gid']		=	intval($gid);
		$map['gname']	=	$this->getGroupName($gid);
		$result	=	$this->add($map);
		return $result;
	}

	//删除一个用户组
	public function deleteGroup($gid=190){
		$map['gid'] = intval($gid);
		$result = $this->where($map)->delete();
		return $result;
	}

	//获取组ID
	public function getGroupId($gname) {
		//return model('Xtag')->getTagId($gid);
		//韦心红修改 2010-5-31
		return $this->where("gname=$gname")->getField('gid');
	}

	//获取组Name
	public function getGroupName($gid) {
		//return model('Xtag')->getTagName(intval($gid));
		//韦心红修改 2010-5-31
		return $this->where("gid=$gid")->getField('gname');		
	}
}
?>